package com.m.system.controller;

import org.springframework.web.bind.annotation.RestController;

import com.client.job.utils.DataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.m.system.domain.SysRoleEntity;
import com.m.system.service.SysRoleService;
import com.m.utils.PageSearchDomain;
import com.m.utils.UserUtil;
import com.m.utils.ValidResult;
import com.m.utils.ValidationUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 角色信息表Controller
 * 
 * @author monxz
 * @version 2021-01-29
 */
@RestController
@RequestMapping(value = "user/sysRole")
@Api(tags = "角色信息表")
public class SysRoleController{

	@Autowired
	private SysRoleService sysRoleService;

	@PostMapping(value = { "/list" })
	public DataResult list(@RequestBody  PageSearchDomain<SysRoleEntity>  pageSearch) throws Exception {
		PageHelper.startPage(pageSearch.getPageNum(),pageSearch.getPageSize());
		PageInfo<SysRoleEntity> page = new PageInfo<>(sysRoleService.findSysRoleList(pageSearch.getObj()));
		return DataResult.buildSuccess(page);
	}

	@PostMapping(value = "/save")
	public DataResult save(@RequestBody SysRoleEntity sysRolePojo) throws Exception {
		// 校验接口参数
		if (sysRolePojo.getRoleSort() <= UserUtil.getUserMaxRoleLevel(null)) {
			return DataResult.buildFail("不得修改角色级别高于您的数据");
		}

		// 校验接口参数
		ValidResult validResult = ValidationUtils.validateGroup(sysRolePojo);
		if (validResult.isHasErrors()) {
			return DataResult.buildFail(validResult.getErrorMsg());
		}
		
		SysRoleEntity  ckRole = new SysRoleEntity();
		ckRole.setRoleName(sysRolePojo.getRoleName());
		if(sysRoleService.checkUnion(ckRole) == 1) {
			return DataResult.buildFail("角色名称重复");
		}
		return DataResult.buildByInter(sysRoleService.addSysRole(sysRolePojo));
	}

	@PostMapping(value = "/update")
	public DataResult update(@RequestBody SysRoleEntity sysRolePojo) throws Exception {
		// 主键判断
		if (sysRolePojo.getRoleId() == null) {
			return DataResult.buildFail("没有对应的Id");
		}

		// 级别判断
		if (sysRolePojo.getRoleSort() < UserUtil.getUserMaxRoleLevel(null)) {
			return DataResult.buildFail("不得修改角色级别高于您的数据");
		}
		

		// 校验接口参数
		ValidResult validResult = ValidationUtils.validateGroup(sysRolePojo);
		if (validResult.isHasErrors()) {
			return DataResult.buildFail(validResult.getErrorMsg());
		}
		
		SysRoleEntity  ckRole = new SysRoleEntity();
		ckRole.setRoleName(sysRolePojo.getRoleName());
		ckRole.setRoleId(sysRolePojo.getRoleId());
		if(sysRoleService.checkUnion(ckRole) == 1) {
			return DataResult.buildFail("角色名称重复");
		}

		return DataResult.buildByInter(sysRoleService.modifySysRole(sysRolePojo));

	}

	@PostMapping(value = "/delete")
	public DataResult delete(@RequestBody String ids) throws Exception {
		if (ids == null || ids.trim().length() == 0) {
			return DataResult.buildFail("参数不能为空");
		}
		return DataResult.buildByInter(sysRoleService.deleteFalse(ids));
	}

	

}